#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int a[N];
int id(char c) {
  if (c == '0')
    return 10;
  else if (isdigit(c))
    return c - '0';
  else if (c == 'A')
    return 1;
  else if (c == 'J')
    return 11;
  else if (c == 'Q')
    return 12;
  else
    return 13;
}
int cnt[14];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  deque<int> q[14];
  rep(i, 1, 13) rep(j, 1, 4) {
    char c;
    cin >> c;
    if (i != 13)
      q[i].push_front(id(c));
    else
      q[i].push_back(id(c));
  }
  int i = 13, ans = 0;
  while (q[i].size()) {
    int j = q[i].front();
    q[i].pop_front();
    i = j;
    if (++cnt[i] == 4) ans++;
  }
  cout << ans - 1;
  return 0;
}